package com.cd.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.cd.bean.News;
import com.cd.bean.Science;
import com.cd.service.science.ScienceService;
import com.cd.utils.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 科研成果
 *@Author zhk
 *@Date 2017-12-20
 **/
@RequestMapping("/science")
@Controller
public class ScienceController {

	@Autowired
	ScienceService scienceService;
	
	/**
	 * 去编辑界面
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goEdit")
	public ModelAndView goEdit(@RequestParam(value="id")Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		Science science = scienceService.findById(id);
		mv.addObject("science", science);
		mv.setViewName("admin/kycg/science_edit");
		return mv;
	}
	
	/**
	 * 去添加页面
	 * @return
	 */
	@RequestMapping("/goAdd")
	public ModelAndView goAdd() throws Exception {
		ModelAndView mv=new ModelAndView();
		mv.setViewName("admin/kycg/science_add");
		return mv;
	}
	
	/**
	 * 新增(后台)
	 * @param title
	 * @param content
	 * @param level
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public ModelAndView save(@RequestParam(value="SCIENCE_TITLE") String title,@RequestParam(value="SCIENCE_CONTENT") String content,
			                 @RequestParam(value="SCIENCE_LEVEL")Integer level,@RequestParam(value="SCIENCE_RESOURCE") String resource) throws Exception { 
		//int total = scienceService.size();
		ModelAndView mv = new ModelAndView();
		Science science = new Science();
		//science.setSCIENCE_ID(total+1);
		science.setSCIENCE_TITLE(title);
		science.setSCIENCE_CONTENT(content);
		science.setSCIENCE_LEVEL(level);
		science.setPUBLISH_DATE(new Date());
		science.setSCIENCE_RESOURCE(resource);
		scienceService.save(science);
		mv.setViewName("redirect:list.do");
		return mv;
	}

	/**
	 * 	编辑（后台）
	 * @param id
	 * @param title
	 * @param content
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/edit")
	public ModelAndView eidt(@RequestParam(value="SCIENCE_ID")Integer id,@RequestParam(value="SCIENCE_TITLE")String title,
			                 @RequestParam(value="SCIENCE_CONTENT")String content ) throws Exception {
			ModelAndView mv = new ModelAndView();
			Science science = scienceService.findById(id);
			science.setSCIENCE_TITLE(title);
			science.setSCIENCE_CONTENT(content);
			science.setPUBLISH_DATE(new Date());
			scienceService.edit(science);
			mv.setViewName("redirect:list.do");
		return mv;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public ModelAndView delete(@RequestParam(value="id")Integer id) throws Exception{
		ModelAndView mv = new ModelAndView();
		scienceService.delete(id);
		mv.setViewName("redirect:list.do");
		return mv;
	}
	
	
	/**
	 * 去查看内容页面(后台)
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goContent")
	public ModelAndView goContent(@RequestParam(value="id")Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		Science science = scienceService.findById(id);
		mv.addObject("science", science);
		mv.setViewName("admin/kycg/science_content");
		return mv;
	}
	
	/**
	 * 查询（带分页）
	 * @param pn
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listScience")
	public ModelAndView findAllScience(@RequestParam(value = "pn",defaultValue="1")Integer pn) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageHelper.startPage(pn, 15);//开始分页
	    List<Science> list = scienceService.listAll();
		PageInfo page = new PageInfo(list,5);//设置分页信息
		mv.addObject("pageInfo", page);
		mv.addObject("list", list);
		mv.setViewName("info/kycg/kycg");
		return mv;
	}
	
	/**
	 * 通过id查询
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findById")
	public ModelAndView findById(@RequestParam(value = "id") Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		Science sci = null;
		Science nextsci = null;
		Science presci = null;
		int total = scienceService.size();//获取总记录
		int nextId = id+1;
		int preId = id-1;
		if(id <= total){
			if(id >= 1){
				sci = scienceService.findById(id);//当前条
				presci = scienceService.findById(preId);//前一条
				nextsci = scienceService.findById(nextId);//下一条	
			}else {
				sci = scienceService.findById(id+1);
			}
		}else{
			sci = scienceService.findById(id-1);
		}
		mv.addObject("sci", sci);
		mv.addObject("presci", presci);
		mv.addObject("nextsci", nextsci);
		mv.setViewName("info/kycg/kycg_show");
		return mv;
	}
	/**
	 *  科研成果list（后台）
	 * @param pn
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(value = "pn",defaultValue="1")Integer pn) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageHelper.startPage(pn, 10);//开始分页,每页10条记录
	    List<Science> list = scienceService.listAll();
		PageInfo page = new PageInfo(list,5);//设置分页信息
		mv.addObject("pageInfo", page);
		mv.setViewName("admin/kycg/science_list");
		return mv;
	}
	/**
	 * 
	 * 批量删除
	 * @param request
	 * @param response
	 */
	@RequestMapping("/del")  
	public void batchDeletes(HttpServletRequest request, HttpServletResponse response) {  
	    String items = request.getParameter("delitems");
	    System.out.println(items);  
	    String[] strs = items.split(",");  
	  
	    for (int i = 0; i < strs.length; i++) {  
	        try {  
	            int a = Integer.parseInt(strs[i]);  
	            scienceService.delete(a);  
	        } catch (Exception e) {  
	        }  
	    }  
	} 
	
	
}
